package com.yangyouli.unity_demo_server.controller;

import com.alibaba.fastjson.JSONObject;
import com.yangyouli.unity_demo_server.annotation.ResponseClass;
import com.yangyouli.unity_demo_server.annotation.ResponseMethod;
import com.yangyouli.unity_demo_server.server.serializeModel.DamageData;
import com.yangyouli.unity_demo_server.server.serializeModel.MonsterStatus;
import com.yangyouli.unity_demo_server.util.ThreadLocalHelper;

@ResponseClass("monster")
public class MonsterController {

	/**
	 * 将怪物添加到房间的怪物列表中
	 * @param data
	 */
	@ResponseMethod("create")
	public void createMonster (String data) {
		//System.out.println("创建怪物：" + ThreadLocalHelper.getClient().getUserId() + "**" +data);
		String[] monsterIdAndHp = data.split("-");
		ThreadLocalHelper.getClient().getRoomObj().addMonster(monsterIdAndHp[0], 
				new MonsterStatus(monsterIdAndHp[0], Integer.valueOf(monsterIdAndHp[1])));
	}
	
	/**
	 * 将怪物的攻击目标转发给其他玩家
	 * @param data
	 * @return
	 */
	@ResponseMethod("sync")
	public String syncMonster (String data) {
		//System.out.println("转移攻击目标:" + data);
		return data;
	}
	
	/**
	 * 对怪物造成伤害，返回剩余生命值
	 * @param data
	 */
	@ResponseMethod("hit")
	public String hitMonster (String data) {
		DamageData damageData = JSONObject.parseObject(data, DamageData.class);
		int hp = ThreadLocalHelper.getClient().getRoomObj()
				.damageMonster(damageData.getId(), damageData.getDamage());
		MonsterStatus monsterStatus = new MonsterStatus(damageData.getId(), hp);
		return JSONObject.toJSONString(monsterStatus);
	}
}
